﻿#region
/**
 * $Date: 0001/1/1
*/
using System;
using System.Data;
using System.Data.SqlClient;
using System.Collections.Generic;
using System.Linq;
using BuXiaChu.OrderDishes.DAL.DataAccess;
using BuXiaChu.OrderDishes.Model;
#endregion

namespace BuXiaChu.OrderDishes.DAL
{
    public class ShareCodeClassDAL
    {
        #region private const
        private const string PARAM_CLASS_I = "@ClassID";
        private const string PARAM_NAME = "@Name";
        private const string PARAM_CREATE_DATE = "@CreateDate";
        private const string PARAM_LAST_UPDATE_DATE = "@LastUpdateDate";
        private const string PARAM_START = "@Start";
        private const string PARAM_COUNT = "@Count";

        private static readonly string ConnectionString = SQLHelper.CONNECTION_STRING;
        #endregion

        private static void FillModelFrom(SqlDataReader reader, ShareCodeClassModel obj)
        {
            #region
            if (reader != null && !reader.IsClosed)
            {
                obj.ClassID = reader.IsDBNull(reader.GetOrdinal("ClassID")) ? 0 : reader.GetInt64(reader.GetOrdinal("ClassID"));
                obj.Name = reader.IsDBNull(reader.GetOrdinal("Name")) ? String.Empty : reader.GetString(reader.GetOrdinal("Name"));
                obj.CreateDate = reader.IsDBNull(reader.GetOrdinal("CreateDate")) ? DateTime.MinValue : reader.GetDateTime(reader.GetOrdinal("CreateDate"));
                obj.LastUpdateDate = reader.IsDBNull(reader.GetOrdinal("LastUpdateDate")) ? DateTime.MinValue : reader.GetDateTime(reader.GetOrdinal("LastUpdateDate"));
            }
            #endregion
        }

        private static SqlParameter[] GetShareCodeClassParams(ShareCodeClassModel obj)
        {
            #region
            SqlParameter[] dbParams ={					
					 SQLHelper.MakeParam(PARAM_CLASS_I, SqlDbType.BigInt,0,obj.ClassID),
					 SQLHelper.MakeParam(PARAM_NAME, SqlDbType.NVarChar, 100,obj.Name)
			};

            return dbParams;
            #endregion
        }

        public static bool Save(ShareCodeClassModel obj)
        {
            #region
            try
            {
                SqlParameter[] dbParams = GetShareCodeClassParams(obj);

                if (obj.ClassID == 0)
                {
                    obj.ClassID = Convert.ToInt32(SQLHelper.ExecuteScalar(ConnectionString, CommandType.StoredProcedure, "ShareCodeClass_Add", dbParams));
                    return obj.ClassID > 0 ? true : false;
                }
                else
                {
                    var affectNum = SQLHelper.ExecuteNonQuery(ConnectionString, CommandType.StoredProcedure, "ShareCodeClass_Update", dbParams);
                    return affectNum > 0 ? true : false;
                }
            }
            catch (Exception e)
            {
                Exception ex = new Exception("ShareCodeClass-->InsertOrUpdate-->" + e.Message);

                ExceptionMessageDAL.Record(ex);
            }

            return false;
            #endregion
        }

        public static ShareCodeClassModel GetByShareCodeClassId(long classID)
        {
            #region
            SqlDataReader reader = null;
            ShareCodeClassModel obj = new ShareCodeClassModel();

            try
            {
                SqlParameter[] dbParams = {
                        SQLHelper.MakeParam(PARAM_CLASS_I, SqlDbType.Int, 0, classID)
                    };

                reader = SQLHelper.ExecuteReader(ConnectionString, CommandType.StoredProcedure, "ShareCodeClass_GetByClassId", dbParams);

                if (reader.Read())
                {
                    FillModelFrom(reader, obj);
                }
            }
            catch (Exception e)
            {
                Exception ex = new Exception("ShareCodeClass-->GetSingleObject-->" + e.Message);

                ExceptionMessageDAL.Record(ex);
            }
            finally
            {
                if (reader != null && !reader.IsClosed)
                    reader.Close();
            }

            return obj;
            #endregion
        }

        public static List<ShareCodeClassModel> GetShareCodeClassList()
        {
            #region
            var sqlSelect = "select * from ShareCodeClass";
            var result = new List<ShareCodeClassModel>();
            using (SqlDataReader reader = SQLHelper.ExecuteReader(ConnectionString, CommandType.Text, sqlSelect, null))
            {
                while (reader.Read())
                {
                    var obj = new ShareCodeClassModel();
                    FillModelFrom(reader, obj);
                    result.Add(obj);
                }
            }
            return result;
            #endregion
        }

        public static bool Delete(long classID)
        {
            #region
            try
            {
                SqlParameter[] dbParams =
                {
						 SQLHelper.MakeParam(PARAM_CLASS_I, SqlDbType.BigInt,0,classID)
				};
                var AffectRows = SQLHelper.ExecuteNonQuery(ConnectionString, CommandType.StoredProcedure, "ShareCodeClass_Delete", dbParams);
                return AffectRows > 0 ? true : false;
            }
            catch (Exception e)
            {
                Exception ex = new Exception("ShareCodeClass-->DeleteShareCodeClass-->" + e.Message);
                ExceptionMessageDAL.Record(ex);
            }
            return false;
            #endregion
        }
    }
}
